package org.redr.canvas;

import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Map;

import javax.xml.parsers.ParserConfigurationException;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.redr.widgetGUI.settings.CoreSettings;
import org.redr.widgetGUI.settings.RedRSettings;
import org.rosuda.REngine.Rserve.RserveException;

import com.trolltech.qt.core.Qt;
import com.trolltech.qt.gui.QApplication;
import com.trolltech.qt.gui.QMessageBox;
import com.trolltech.qt.gui.QPixmap;
import com.trolltech.qt.gui.QSplashScreen;

public class RedRApplication extends QApplication{
	
	static RedRSettings settings;
	static CoreSettings coreSettings;
	public static File parentDir;
	public static File settingsDir;
	public static QSplashScreen splashWindow = null;
	
	
	public RedRApplication(String[] args) {
		super(args);
		// TODO Auto-generated constructor stub
	}
	
	public static void setUp(){
		Map<String, String> env = System.getenv();
		for ( String k : env.keySet() ){
			System.out.println(k + ": " + env.get(k));
		}
		parentDir = new File(System.getenv().get("RRJAVADIR"));
		QPixmap logo = new QPixmap(OSPath.join(parentDir, "coreIcons", "splash.png").toString());
		splashWindow = new QSplashScreen(logo, Qt.WindowType.WindowStaysOnTopHint);
		splashWindow.setMask(logo.mask());
        splashWindow.show();
        
        System.out.println("Parent dir is:" + parentDir);
		settingsDir = new File(System.getProperties().get("user.home").toString(), ".rrjava");
		try {
			settings = new RedRSettings(); // makes the settings for a blank document
		} catch (ParserConfigurationException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			QMessageBox.warning(null, "Error", ExceptionUtils.getStackTrace(e1));
			System.exit(5);
		}
		try {
			coreSettings = new CoreSettings(OSPath.join(settingsDir, "coreSettings", "coreSettings.xml"));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			QMessageBox.warning(null, "Error", ExceptionUtils.getStackTrace(e));
			System.exit(5);
		}
		
		System.out.println("Registering widgets");
		
		splashWindowShowMessage("Registering Widgets", Qt.AlignmentFlag.AlignHCenter.value() + Qt.AlignmentFlag.AlignBottom.value());
		RedRObjects.registerWidgets();
		splashWindowShowMessage("Registering Templates", Qt.AlignmentFlag.AlignHCenter.value() + Qt.AlignmentFlag.AlignBottom.value());
		RedRObjects.registerTemplates();
		
		System.out.println("Done with settings.");
	}
	
	public static boolean saveDocument(){
		return true;
	}
	
	public void loadDocument(){
		
	}
	
	public void shutdown() throws Exception {
		
	}
	
	

	public static File getParentDir(URL url) {
		// TODO Auto-generated method stub
		File tempFile;
		try{
			tempFile = new File(url.toURI());
		} catch (URISyntaxException e) {
			tempFile = new File(url.getPath());
		}
		while (!tempFile.getName().equals("bin")){
			tempFile = tempFile.getParentFile();
		}
		return tempFile.getParentFile();
	}

	/**
	 * @param args
	 * @throws RserveException 
	 */
	public static void main(String[] args){
		// TODO Auto-generated method stub
		try{
			QApplication.initialize(args);
			RedRApplication.setUp();
			
			splashWindowShowMessage("Starting R Session", Qt.AlignmentFlag.AlignHCenter.value() + Qt.AlignmentFlag.AlignBottom.value());
			
			RSession.startUp();
			RedRCanvas widget = new RedRCanvas();
			widget.show();
			splashWindow.hide();
			
			QApplication.exec();
		} catch (Exception e){
			e.printStackTrace();
		}
	}
	
	/**
	 * Loads a file into the application.  This is a global effect, and therefore, takes place in the global level.
	 * @param name
	 */
	public void loadFile(String name) {
		// clean up the current file
		
		// unzip/unpack the previous file
		
		// set the data
		
	}
	
	public static CoreSettings coreSettings(){
		return coreSettings;
	}

	public static String _(String string) {
		// TODO Auto-generated method stub
		return string;
	}

	public static void loadDocument(File f) {
		// TODO integrate with the loadFile command...
		
	}

	public static void clearCanvas() {
		// TODO Auto-generated method stub
		
	}

	public static void saveDocumentAs() {
		// TODO Auto-generated method stub
		
	}

	public static void saveTemplate() {
		// TODO Auto-generated method stub
		
	}

	public static void splashWindowShowMessage(String message, int i) {
		// TODO Auto-generated method stub
		if (splashWindow != null){
			splashWindow.showMessage(message, i);
		}
		
	}

}
